package top.lshaci.learning.springboot.jpa.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lshaci.learning.springboot.jpa.entity.Person;
import top.lshaci.learning.springboot.jpa.repository.PersonRepository;

import java.util.Optional;

/**
 * ServiceA
 *
 * @author liangxu
 */
@Service
public class ServiceA {

    @Autowired
    ServiceB serviceB;

    @Autowired
    PersonRepository personRepository;

    @Transactional(rollbackFor = Exception.class)
    public void exec() {
        Optional<Person> personOptional = personRepository.findById(10L);
        if (personOptional.isPresent()) {
            Person person = personOptional.get();
            person.setAge(18);
            personRepository.saveAndFlush(person);
        }
        // Lock wait timeout exceeded; try restarting transaction
        serviceB.exec();
    }
}
